
var apiConfig={
    "共通表格查询":{uri:"/common-table",method:"get"},
    "共通表格编辑":{uri:"/common-table",method:"put"},
    "我是其他":{uri:"/other",method:"put"},
    "泳池现场采样管理查询":{uri:"/tswimmingpoolsamplingrecord/detail",method:"get"},
    "泳池现场采样管理增删改":{uri:"/tswimmingpoolsamplingrecord",method:"put"},
}

var serverAddr="http://192.168.2.55:8021";


/**
 * 获取表格数据
 * @param {string} type - 请求类型
 * @param {pathParam:map,requestParam:map,data:json} paramObj - 参数映射表
 * 
 * @param {function} callback - 回调函数
 */
function fetchData(type,paramObj,callback) {
    xhrReqeust(type,paramObj,callback)
}


function submitData(type,paramObj,callback){
    //比对差异数据

    //提交标记的数据
    xhrReqeust(type,paramObj,callback)
}


function xhrReqeust(type,paramObj,callback){
    if(callback==undefined){
        alert("回调函数不能为空!!!")
        return
    }
    var api=apiConfig[type]
    var xhr = new XMLHttpRequest();
    var url=serverAddr+trsfFinalPath(paramObj.pathParam,api);
    if(paramObj.requestParam!=undefined){
        url+="?"
        for(var key in paramObj.requestParam){
            url+=(key+"="+paramObj.requestParam[key]+"&")
        }
    }
    console.log("【准备请求路径】"+url)
    xhr.open(api.method, url, true);
    xhr.onload = function () {
    if (this.status === 200) {
        var data = JSON.parse(this.responseText);
        if(callback!=undefined){
            callback(data)
        }
    } else {
        console.error('请求失败：' + this.status);
    }
    };
    xhr.onerror = function () {
        console.error('请求过程中出现错误');
    };
    if(api.method!="get" && paramObj.data!=undefined){
        console.log("待提交数据:",JSON.stringify(paramObj.data))
        xhr.setRequestHeader("Content-Type", "application/json");
        xhr.send(JSON.stringify(paramObj.data));
    }else{
        xhr.send();
    }

}

function trsfFinalPath(pathParamMap,apiObj){
    if(pathParamMap==undefined || apiObj.uri.indexOf("{")==-1){
        return apiObj.uri;
    }else{
        var tmpUri=apiObj.uri;
        for(var key in pathParamMap){
            if(tmpUri.indexOf("{"+key+"}")==-1) continue;
            tmpUri=tmpUri.replace("{"+key+"}",pathParamMap[key]);
        }
        return tmpUri
    }    

}



